package com.template.server.common.exception;

import com.template.server.common.result.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author liuxf
 * @date 2024/4/12
 * 全局异常处理
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(value = BizException.class)
    public R bizExceptionHandler(BizException e) {
        log.error("业务异常：" + e.getMessage(), e);
        return R.fail(e.getMessage());
    }

    @ExceptionHandler(value = UnAuthException.class)
    public R unAuthExceptionHandler(UnAuthException e) {
        log.error("未认证", e);
        return R.fail(e.getMessage());
    }

    @ExceptionHandler(value = UnPermissionException.class)
    public R unPermissionExceptionHandler(UnPermissionException e) {
        log.error("未授权", e);
        return R.fail(e.getMessage());
    }

    @ExceptionHandler(value = SystemErrorException.class)
    public R systemErrorExceptionHandler(SystemErrorException e) {
        log.error(e.getMessage(), e);
        return R.error(e.getStatusCode(), e.getMessage());
    }

    @ExceptionHandler(value = Exception.class)
    public R exceptionHandler(Exception e) {
        log.error("系统异常：" + e.getMessage(), e);
        return R.error();
    }

}
